package com.hong.servlet;


import com.alibaba.fastjson.JSON;
import com.hong.entity.Oss;
import com.hong.service.Impl.OssServiceImpl;
import com.hong.service.OssService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Oss.action")
public class OssServlet extends HttpServlet {
    OssService ossService = new OssServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = (request.getParameter("opr") == null || "".equals(request.getParameter("opr"))) ? "list" : request.getParameter("opr");
        switch (opr){
            //查询所有存储
            case "list":
                List<Oss> ossList = ossService.selectAll();
                String json = JSON.toJSONString(ossList);
                response.getWriter().print(json);
                return;
            //根据名称查询信息
            case "ossListName":
                String bucketName = request.getParameter("bucketName");
                Oss oss = ossService.ossListName(bucketName);
                String jsonOne = JSON.toJSONString(oss);
                response.getWriter().print(jsonOne);
                return;
            case "add":
                String bucketNameOne = request.getParameter("bucketName");
                String type = request.getParameter("type");
                String permission = request.getParameter("permission");

                Oss ossOne = new Oss();
                ossOne.setBucketName(bucketNameOne);
                ossOne.setType(type);
                ossOne.setPermission(permission);
                return;

            case "update":
                String bucketNameTwo = request.getParameter("id");
                String permissionOne = request.getParameter("permission");
                boolean updateOss = ossService.updatePermission(bucketNameTwo,permissionOne);
                String jsonTwo = JSON.toJSONString(updateOss);
                response.getWriter().print(jsonTwo);
        }
    }
}
